<?php

namespace Home\Controller;

class StoreHouseController extends HomeController {
	
    /**
     * 仓库首页(non-PHPdoc)
     * @see \Home\Controller\HomeController::index()
     */
	function index() {
		
	    $map = array();
	    if (!empty($_REQUEST['type'])) {
	        $map['type'] = array('eq',  $_REQUEST['type']);
	    }
	    if (!empty($_REQUEST['product_id'])) {
	        $map['product_id'] = array('eq',  $_REQUEST['product_id']);
	    }
	    if (!empty($_REQUEST['product_type_id'])) {
	        $map['product_type_id'] = array('eq',  $_REQUEST['product_type_id']);
	    }
	    $type = empty($_REQUEST['type']) ? '' : $_REQUEST['type'];
	    $product_id = empty($_REQUEST['product_id']) ? '' : $_REQUEST['product_id'];
	    $product_type_id = empty($_REQUEST['product_type_id']) ? '' : $_REQUEST['product_type_id'];
	    $product = M('storehouse_product')->field('id,product')->select();
	    $productType = M('storehouse_product_type')->where('is_del=0')->field('id,type_name')->select();
	    $model = M('storehouse');
	    $this -> assign('auth', $this -> config['auth']);
	    $this -> _list($model, $map);
	    $this -> assign('product',$product);
	    $this -> assign('type',$type);
	    $this -> assign('product_id',$product_id);
	    $this -> assign('product_type_id',$product_type_id);
	    $this -> assign('product_type',$productType);
	    $this->display();
	}
	
	 /**
	  * 详情(non-PHPdoc)
	 * @see \Home\Controller\HomeController::read()
	 */
	function read($id) {
        
	    if (empty($id)) {
	    	R('StoreHouse/index');
	    }
	    $storeHouse = M('storehouse');
	    $map['id'] = $id;
	    $data = $storeHouse->where($map)->find();
	    $this->assign('list',$data);
	    $this -> assign('auth', $this->config['auth']);
	    $this -> assign('cr', ($data['type']==1 ? '入库':'出库'));
	    $this->display();
	}
	
	/**
	 * 删除
	 * @param unknown $id
	 */
	public function delete($id) {

	    $storeHouse = M('storehouse');
	    $map['id'] = $id;
	    $status = $storeHouse->where($map)->delete();
	    if ($status) {
	        $this -> success("成功删除1条!");
	    } else {
	        $this -> error('删除失败!');
	    }
	}
	
	/**
	 * 增加入库
	 */
	function inc() {
	    
	    $storeProduct = M('storehouse_product');
	    $product = $storeProduct->getField("id,product,product_bh");
	    $this->assign('opreate_name',get_user_name());
	    $this->assign('product',$product);
	    $this->display();
	}
	
	 /**
	  * 添加
	 */
	function addPost() {
		
	    $d = $_POST;
	    $this->validate($d);
	    $storeProduct = M('storehouse_product');
	    $productType = $storeProduct->field('id,product,product_bh,product_type_id,product_type_name')->where('id='.$d['storehouse_product_id'])->find();
	    if(empty($productType)) {
	       $this->output(false,'请输产品');
	    }
	    if(!empty($_FILES['product_img']['name'])) {
	       $upload = new \Think\Upload();// 实例化上传类
	       $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
	       $upload->rootPath  =     './Uploads/store/'; // 设置附件上传根目录
	       $info = $upload->upload();// 上传文件
	       if (!$info) { // 上传错误提示错误信息 
	           $this->output(false,'上传图片失败');
	       }
	    }
	    $storeHouse = M('storehouse');
	    $data = array(
	        'product_id'=>$productType['id'],
            'product_name' => $productType['product'],
            'product_bh' => $productType['product_bh'],
	        'product_type_id' => $productType['product_type_id'],
	        'product_type_name' => $productType['product_type_name'],
	        'number'  => $d['number'],
	        'type' => $d['type'],
	        'user_id' => get_user_id(),
	        'user_name' => get_user_name(),
	        'opreate_name' => $d['opreate_name'],
	        'remark' => $d['remark'],
	        'update_at' => date('Y-m-d H:i:s',time()),
	        'create_at' => date('Y-m-d H:i:s',time())
	    );
	    if (!empty($info['product_img']['savepath']) && !empty($info['product_img']['savename'])) {
	        $data['product_img'] = $info['product_img']['savepath'].$info['product_img']['savename'];
	    }
	    $storeHouse->startTrans();
	    $status = $storeHouse->add($data);
	    if ($status) {
	        $storehouse_product = M('storehouse_product');
	        if ($d['type']==1) {        // 入库
	            $flag = $storehouse_product -> where('id='.$d['storehouse_product_id']) -> setInc('num',$d['number']);
	            $gg = $storehouse_product -> where('id='.$d['storehouse_product_id']) -> setInc('in_num',$d['number']);
	        } else if ($d['type']==2) { //  出库
	            $flag = $storehouse_product -> where('id='.$d['storehouse_product_id']) -> setDec('num',$d['number']);
	            $gg = $storehouse_product -> where('id='.$d['storehouse_product_id']) -> setInc('out_num',$d['number']);
	        }
	    	if ($status && $flag) {
	    	    $storeHouse->commit();
	    	    $this -> success("添加成功",U('index'));
	    	} else {
	    	    $storeHouse->rollback();
	    	    $this -> error("添加失败",U('index'));
	    	}
	    } else {
	        $storeHouse->rollback();
	        $this -> error("添加失败",U('index'));
	    }
	    
	}
	
	/**
	 * 编辑
	 */
	public function editPost() {
	    
	    $d = $_POST;
	     if (empty($d['id'])) {
	        $this->output(false,'请选择编辑产品');
	    }
	    $this->validate($d);
	    $storeProduct = M('storehouse_product');
	    $productType = $storeProduct->field('id,product,product_bh')->where('id='.$d['storehouse_product_id'])->find();
	    if(empty($productType)) {
	        $this->output(false,'请输产品');
	    }
	    $storeHouse = M('storehouse');
	    $map['id'] = $d['id'];
	    $data = array(
            'product' => $productType['product'],
	        'product_bh' => $productType['product_bh'],
            'number'  => $d['number'],
	        'opreate_name' => $d['opreate_name'],
	        'remark' => $d['remark'],
	        'update_at' => date('Y-m-d H:i:s',time()),
	    );
	    $status = $storeHouse->where($map)->save($data);
	    if ($status) {
	        $this->output(true);
	    } else {
	        $this->output(false,'更新失败，请刷新重试');
	    }
	}
	
	/**
	 * 验证
	 * @param unknown $d
	 */
	function validate($d) {
	    
	    if (empty($d['storehouse_product_id'])) {
	        $this->output(false,'请输产品');
	    }
	    if (empty($d['number'])) {
	        $this->output(false,'请输产品数量');
	    }
	    if (!is_numeric($d['number'])) {
	        $this->output(false,'产品数量必须为数字');
	    }
	    if (empty($d['opreate_name'])) {
	        $this->output(false,'请输入操作人');
	    }
	    if (empty($d['remark'])) {
	        $this->output(false,'请输入摘要');
	    }
	}
	
	 /**
	  * 增加出库
	 */
    function dec() {
        
        $storeProduct = M('storehouse_product');
        $product = $storeProduct->getField("id,product,product_bh,product_img");
        $this->assign('opreate_name',get_user_name());
        $this->assign('product',$product);
	    $this->display();
    }
    
    /**
      * 入库的产品
    */
    function product() {
        
        $this -> assign('auth', $this -> config['auth']);
        $model = M("storehouse_product");
        $map = $this -> _search($model);
        $this -> _list($model, $map);
        $productType = M('storehouse_product_type')->where('is_del=0')->field('id,type_name')->select();
        $this->assign('product_type',$productType);
        $this-> assign('product',$_REQUEST['li_product']);
        $this-> assign('product_bh',$_REQUEST['li_product_bh']);
        $this-> assign('product_type_id',$_REQUEST['eq_product_type_id']);
        $this->display();
    }
    
    /**
     * 库存的产品类型
     */
    function product_type() {

    	$model = M("storehouse_product_type");
    	$map = $this -> _search($model);
    	$this -> _list($model, $map);
    	$this -> assign('auth', $this -> config['auth']);
    	$this->display();
    }
    
    /**
     * 添加入库产品
    */
    function addProduct() {
        
        $productType = M('storehouse_product_type')->where('is_del=0')->field('id,type_name')->select();
        $this->assign('product_type',$productType);
        $this->display();
    }
    
    /**
     * 添加库存类别
     */
    function addProductType() {
            
        $this->display();
    }
    
    /**
     * 添加库存的类别
    */
    function addProductTypePost() {
            
        $d = $_POST;
        if (empty($d['type_name'])) {
            $this->output(false,'请输入库存类别名称');
        }
        $data = array(
            'type_name' => $d['type_name'],
            'user_id' => get_user_id(),
            'create_at' =>  date('Y-m-d H:i:s',time())
        );
        $storeProductType = M('storehouse_product_type');
        $status = $storeProductType->add($data);
        if ($status) {
            $this -> success("添加成功",U('product_type'));
        } else {
            $this -> error("添加失败",U('addProductType'));
        }
    }
    
     /**
      * 添加产品
     */
    function addProductPost(){

        $d = $_POST;
        if (empty($d['product_type_id'])) {
            $this -> error("添加失败,请输入库存类别",U('addProduct'));
        }
        if (empty($d['product'])) {
            $this -> error("添加失败,请输入产品",U('addProduct'));
        }
        if (empty($d['product_bh'])) {
            $this -> error("添加失败,请输入产品编号",U('addProduct'));
        }
        $typeWhere = array(
            'id' => $d['product_type_id']	
        );
        $where = array(
            'product' => $d['product'],
            'product_bh' => $d['product_bh'],
        );
        $storeProduct = M('storehouse_product');
        $result = $storeProduct->field('id')->where($where)->select();
        if (!empty($result)) {
            $this -> error("添加失败,该产品型号已经添加",U('addProduct'));
        }
        $storeProductType = M('storehouse_product_type')->where($typeWhere)->field('id,type_name')->find();
        if (empty($storeProductType)) {
            $this -> error("添加失败,库存类别不存在",U('addProduct'));
        }
        $data = array(
            'product' => $d['product'],
            'product_bh' => $d['product_bh'],
            'user_id' => get_user_id(),
            'user_name' => get_user_name(),
            'product_type_id' => $storeProductType['id'],
            'product_type_name' =>  $storeProductType['type_name'],
            'type_name' => $storeProductType['type_name'],
            'create_at' =>  date('Y-m-d H:i:s',time())
        ) ;
        if(!empty($_FILES['product_img']['name'])) {
            $upload = new \Think\Upload();// 实例化上传类
            $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath  =     './Uploads/store/'; // 设置附件上传根目录
            $info   =   $upload->upload();// 上传文件
            if(!$info) {// 上传错误提示错误信息
                $this -> error("上传图片失败",U('product'));
            }
        }
        if (!empty($info['product_img']['savepath']) && !empty($info['product_img']['savename'])) {
            $data['product_img'] = $info['product_img']['savepath'].$info['product_img']['savename'];
        }
        $status = $storeProduct->add($data);
        if ($status) {
            $this -> success("添加成功",U('index'));
        } else {
            $this -> error("添加失败",U('addProduct'));
        }
        
    }
    
    /**
     * 删除库存的产品类别
     */
    function deleteType() {
                
        $id = $_POST['id'];
        if(empty($id)) {
            $this->output(false,'删除失败');
        }
        $product_type = M('storehouse_product_type');
        $map['id'] = $id;
        $status = $product_type->where($map)->delete();
        if ($status) {
            $this->output(true,'删除成功');
        } else {
            $this->output(false,'删除失败');
        }         
    }
    
    /**
     * 编辑库存的产品的类别
    */
    function editType($id) {
            
        if (empty($id)) {
            R('StoreHouse/product_type');
        }
        $product_type = M('storehouse_product_type');
        $map['id'] = $id;
        $data = $product_type->where($map)->find();
        $this->assign('result',$data);
        $this -> assign('auth', $this->config['auth']);
        $this->display();
    }
    
     /**
     * 删除入库产品
     * @param unknown $id
     */
    function delProduct() {
    	
    	$id = $_POST['id'];
    	if(empty($id)) {
    		$this->output(false,'删除失败');
    	}
    	$product = M('storehouse_product');
        $map['id'] = $id;
        $status = $product->where($map)->delete();
        if ($status) {
        	$this->output(true,'删除成功');
        } else {
        	$this->output(false,'删除失败');
        }
    } 
	
     /**
     * 编辑入库产品
     * @param unknown $id
     */
    function editProduct($id) {
		
    	if (empty($id)) {
    		$this->error('请选择编辑的产品');
    	}
    	$product = M('storehouse_product');
    	$map['id'] = $id;
    	$result = $product->where($map)->field('id,product,product_bh,product_img,product_type_id')->find();
    	if (empty($result)) {
    		$this->error('请选择编辑真实有效产品');
    	}
    	$productType = M('storehouse_product_type')->where('is_del=0')->field('id,type_name')->select();
    	$this->assign('result',$result);
    	$this->assign('product_type',$productType);
    	$this->display();
    }
    
    /**
     * 保存入库产品
     */
    function saveProduct(){

    	$d = $_POST;
    	if (empty($d['id'])) {
    		$this->output(false,'请选择编辑的产品');
    	}
    	if (empty($d['product_type_id'])) {
    	    $this -> error("添加失败,请输入库存类别",U('addProduct'));
    	}
    	if (empty($d['product'])) {
        	$this->output(false,'请输入产品');
        }
        if (empty($d['product_bh'])) {
            $this->output(false,'请输入产品编号');
        }
        $typeWhere = array(
            'id' => $d['product_type_id']
        );
        $storeProductType = M('storehouse_product_type')->where($typeWhere)->field('id,type_name')->find();
        if (empty($storeProductType)) {
            $this -> output(false,"添加失败,库存类别不存在");
        }
        $product = M('storehouse_product');
        $map['id'] = $d['id'];
        $data = array(
            'product_type_id' => $storeProductType['id'],
            'product_type_name' =>  $storeProductType['type_name'],
        	'product' => $d['product'],
        	'product_bh' => $d['product_bh']
        );
        $status = $product->where($map)->save($data);
        if ($status) {
        	$this->output(true);
        } else {
        	$this->output(false,'保存失败，请刷新再试');
        }
    }
    
    /**
     * 保存入库产品的类别
    */
    function saveProductTypePost() {

        $d = $_POST;
        
        if (empty($d['id'])) {
            $this->output(false,'请选择编辑的库存类型');
        }
        if (empty($d['type_name'])) {
            $this->output(false,'请输入库存类别');
        }
        $product_type = M('storehouse_product_type');
        $map['id'] = $d['id'];
        $data = array(
            'type_name' => $d['type_name'],
        );
        $status = $product_type->where($map)->save($data);
        if ($status) {
            $this->output(true);
        } else {
            $this->output(false,'保存失败，请刷新再试');
        } 
    }
    
    /**
     * 显示库存
     */
    function showNum() {

        if (empty($_POST['id'])) {
            $this->output(false,'请选择产品');
        }
        $storeProduct = M('storehouse_product');
        $product = $storeProduct->field('id,product,num,in_num,out_num')->where('id='.$_POST['id'])->find();
        if (empty($product)) {
            $this->output(false,'请选择真实有效产品');
        } else {
            $this->output(true,$product['num']);
        }  
    }
}
